/**
 * @File		: CustomKeyListener.java
 * @Subject		: Handles key events
 * @author 		: Isaac Parenteau	locus2k@gmail.com
 * @Date		: October 12th, 2011
 * @Licence		: Licensed under the GPU LGPL
 */
package mil.cdc.listeners;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

import mil.cdc.main.Main;
import mil.cdc.windows.MainWindow;

public class CustomKeyListener implements KeyListener {

	private JFrame currentFrame;
	
	/**
	 * The frame the key listener will be attached to
	 * @param currentFrame Current Frame.
	 */
	public CustomKeyListener(JFrame currentFrame) {
		this.currentFrame = currentFrame;
	}
	
	@Override
	public void keyPressed(KeyEvent event) {
		
		if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
			Main.close();
			currentFrame.dispose();
		}
		
		else if (currentFrame instanceof MainWindow) {
			MainWindow window = (MainWindow)currentFrame;
			if (event.getKeyCode() == KeyEvent.VK_ENTER) {
				window.submitAnswer();
			}
			else if (event.getKeyCode() == KeyEvent.VK_F1) {
				window.showHelp();
			}
			else if (event.getKeyCode() == KeyEvent.VK_F2) {
				window.getPrevQuestion();
			}
			else if (event.getKeyCode() == KeyEvent.VK_F3) {
				window.getNextQuestion();
			}
			else if (event.getKeyCode() == KeyEvent.VK_F5) {
				window.showHowTo();
			}
		}
		else if (event.getKeyCode() == KeyEvent.VK_ENTER) {
			if (currentFrame instanceof MainWindow) {
				((MainWindow)currentFrame).submitAnswer();
			}
		}

	}

	@Override
	public void keyReleased(KeyEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent event) {
		// TODO Auto-generated method stub

	}

}
